<!DOCTYPE html>
<html>
<body>
<?php
/**
 * Created by PhpStorm.
 * User: jiangshuxiong
 * Date: 2018/1/19
 * Time: 18:07
 * 异常的规则：如果抛出了异常，就必须捕获它。
 * 需要进行异常处理的代码应该放入 try 代码块内，以便捕获潜在的异常。
 * 每个 try 或 throw 代码块必须至少拥有一个对应的 catch 代码块。
 * 使用多个 catch 代码块可以捕获不同种类的异常。
 * 可以在 try 代码块内的 catch 代码块中再次抛出（re-thrown）异常
 */
function checkNum($number){
    if($number > 1){
        throw new Exception("Value must below 1......<br />");
    }
    return true;
}

function checkNum1($number,$number2=0){
    if($number > 1 && $number2 == 1){
        throw new Exception("Value must below 2");
    }
    return true;
}
try{
    checkNum(2);
    echo "if you see this,the number is below 1";
}
catch(Exception $e){
    echo "message : " . $e->getMessage();
}

?>

<!--自定义异常-->
<?php
class customException extends Exception
{
    public function errorMessage()
    {
        //error message
        $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
            .': <b>'.$this->getMessage().'</b> is not a valid E-Mail address<br />';
        return $errorMsg;
    }
}

$email = "someone@example...com";

try
{
    //check if
    if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
        //throw exception if email is not valid
        throw new customException($email);
    }
}

catch (customException $e)
{
    //display custom message
    echo $e->errorMessage();
}
?>

<!--多个异常-->
<?php
class customException2 extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address<br />';
return $errorMsg;
}
}

$email = "someone@example.com";

try
 {
 //check if
 if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
  {
  //throw exception if email is not valid
  throw new customException2($email);
  }
 //check for "example" in mail address
 if(strpos($email, "example") !== FALSE)
  {
  throw new Exception("$email is an example e-mail<br />");
  }
 }

catch (customException2 $e)
 {
 echo $e->errorMessage();
 }

catch(Exception $e)
 {
 echo $e->getMessage();
 }
?>


<!--重新抛出异常-->
<?php
class customException1 extends Exception
{
    public function errorMessage()
    {
        //error message
        $errorMsg = $this->getMessage().' is not a valid E-Mail address.<br />';
        return $errorMsg;
    }
}

$email = "someone@example.com";

try
{
    try
    {
        //check for "example" in mail address
        if(strpos($email, "example") !== FALSE)
        {
            //throw exception if email is not valid
            throw new Exception($email);
        }
    }
    catch(Exception $e)
    {
        //re-throw exception
        throw new customException1($email);
    }
}

catch (customException1 $e)
{
    //display custom message
    echo $e->errorMessage();
}
?>

<!--设置顶层异常处理器-->
<?php
function myException($exception)
{
    echo "<b>Exception:</b> " , $exception->getMessage();
}

set_exception_handler('myException');
//此处不需要catch语句
throw new Exception('Uncaught Exception occurred');
?>
</body>
</html>



